lambda

Eric Wilhelm on 2006-09-04T00:39:11

Working on list.pm today and again trying to work around the fact that perl 5 prototypes don't apply to methods (thus, I want to export a handy one-character function which is a shortcut to saying "sub {...}" -- Yes, I'm saving all of two characters and a space. If that doesn't sound worthwhile, move along, nothing to see here.)

First, charge the batteries in your propeller beanie...

I had previously settled on _{...} (e.g. "sub _(&) {$_[0]}"), but then discovered the mysterious globalness of *_ and so now I'm using F{...}, but wanting something slightly more meaningful.

  use utf8;
  sub λ (&) {$_[0]};
  λ{print "foo"}->();

Excellent! No, I'm not putting that in the API just yet, but I'm seriously thinking about it. The only trick is to get the keyboard and/or vim setup worked-out so that it's not any harder to type than s-u-b-<space>.


vim digraphs

rhesa on 2006-09-04T09:01:09

You could turn on vim's digraphs, and type l*.

vim magic for this from my .vimrc:
  set encoding=utf-8
  set digraph
Alternatively, you could use a imap or iab for it, e.g. imap ,\ λ.

Re:vim digraphs

rhesa on 2006-09-04T10:54:07

You could turn on vim's digraphs, and type l*.

Uh, make that ctrl+k l*. That's a lot harder to type than sub , so I'd go for the imap option.

Re:vim digraphs

Eric Wilhelm on 2006-09-05T00:59:49

I haven't taken the plunge in the API quite yet, but I did put this in my .vimrc:

    imap <C-L> λ{}<ESC>:set encoding=utf8<CR>i

And of course, you could just make that type "sub {" for you as well, but lambda reads better :-D

Offer Alternatives

Theory on 2006-09-04T18:28:53

If it's a public interface, maybe offer an alternative?

use utf8;
sub &#955; (&) { shift }
sub n (&) { shift }

—Theory